#define _CRT_SECURE_NO_WARNINGS 1

#include"Queue.h"

void QueueInit(Queue* q)
{
	q->_front = NULL;
	q->_rear = NULL;
}

QNode* BuyQueueNode(QDataType data)
{
	QNode* cur = (QNode*)malloc(sizeof(QNode));
	if (cur == NULL)
	{
		perror("BuyQueueNode-malloc");
		return;
	}
	cur->_data = data;
	cur->_next = NULL;
	return cur;
}

void QueuePush(Queue* q, QDataType data)
{
	QNode* cur = BuyQueueNode(data);
	if (q->_front == NULL)
	{
		q->_front = q->_rear = cur;
	}
	else
	{
		q->_rear->_next = cur;
		q->_rear = cur;
	}
}

void QueuePop(Queue* q)
{
	QNode* temp = q->_front->_next;
	free(q->_front);
	q->_front = temp;
}

QDataType QueueFront(Queue* q)
{
	return q->_front->_data;
}

QDataType QueueBack(Queue* q)
{
	return q->_rear->_data;
}

int QueueSize(Queue* q)
{
	int count = 0;
	QNode* cur = q->_front;
	while (cur)
	{
		count++;
		cur = cur->_next;
	}
	return count;
}

int QueueEmpty(Queue* q)
{
	if (q->_front == NULL)
	{
		return 1;
	}

	return 0;
}

void QueueDestroy(Queue* q)
{
	if (q->_front == NULL)
	{
		return;
	}
	while (q->_front)
	{
		QueuePop(q);
	}
}

